#coding=utf-8
import time  
from selenium import webdriver  
from selenium.webdriver.common.action_chains import ActionChains

import math
import random
import numpy as np
import os
import xlrd
import pandas as pd


def batch_update(account_list, MCC_list):
    
    # log in
    driver = webdriver.Chrome(r'C:\Users\guosu\Desktop\chromedriver_win32\chromedriver.exe')
    elem = driver.get('https://admin.paypal.com')
    elem = driver.find_element_by_id("id_accountNo")
    elem.send_keys(account_list[0])
    elem_search = driver.find_element_by_class_name("findUserSearch").click()

    # define the update function.
    def update(account_id,MCC):

        print("Updating {} time, on account {}".format(str(i+1),MCC))
            
        elem = driver.get('https://admin.paypal.com/cgi-bin/admin?node=supplemental_merchant_info&account_number='+str(account_id))
        driver.execute_script("window.scrollTo(0, (document.body.scrollHeight)/3);")

        js = "document.getElementById('admin_mcc_toggle_status_val').style.display='none';"
        js1 = "document.getElementById('admin_mcc_dynamic_search').style.display='block';"
        js2 = "document.getElementById('admin_mcc').style.display='inline-block';"
        js3 = "document.getElementById('admin_mcc_toggle').style.display='block';"
        js4 = "document.getElementById('admin_mcc_toggle_save').style.display='inline-block';"
        driver.execute_script(js)
        driver.execute_script(js1)
        driver.execute_script(js2)
        driver.execute_script(js3)
        driver.execute_script(js4)
        time.sleep(2)
        driver.find_element_by_id("admin_mcc").send_keys(MCC)
        driver.find_element_by_id("memo_admin_mcc_toggle").send_keys('MCC PROJECT')
        driver.find_element_by_id("admin_mcc_toggle_save").click()
        time.sleep(3)

    for i in range(len(account_list)):
        update(account_list[i], MCC_list[i])
        
    driver.quit()


# get list from excel
df = pd.read_excel("2.xlsx", sheet_name=0)     # add sheetname in so we can put all data in one excel
account_list = list()
MCC_list = list()
print(df)
i = 0
while (i < len(df)) :
    account_list.append(df.iloc[i][0].tolist())
    MCC_list.append(df.iloc[i][1].tolist())
    i = i + 1
i = 0

# check if the list is correct and then run the main function.
if((True not in [np.isnan(i) for i in MCC_list])and(True not in [np.isnan(i) for i in account_list])):
        #b = "https://admin.paypal.com/cgi-bin/admin?node=supplemental_merchant_info&account_number="+str(a)+"#admin_restrictions_new"
        batch_update(account_list,MCC_list)
else:
    print("Sorry, please check your list and make sure each accounts has an MCC code")




#ActionChains(driver).move_by_offset(200, 100).click().perform()   #Click on some elements.

# change by browsers
'''
def browser(browser):
  try:
       if browser=="firefox":
        driver=webdriver.Firefox()
        return driver
       elif browser=="chrome":
           driver=webdriver.Chrome(r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')
           return driver
       else:
           print("Not found browser!You can enter 'firefox','chrome'")
    except Exception as msg:
        print("open browser error:%s"%msg)


if __name__=="__main__":

  # 用谷歌浏览器打开天龟网页
    driver_chrome=browser("chrome")
    driver_chrome.get("http://www.tianguiedu.com")
    # 获取浏览器的名称
    print("open browser: %s"%driver_chrome.name)
    # 获取网页名称
    print(driver_chrome.title)
    time.sleep(5)  #休眠5秒
    driver_chrome.quit() #谷歌浏览器不关闭网页会报错

'''
